<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/24
 * Time: 11:17
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\DataCategory;
use think\Request;
use app\sports_admin\service\BookTypes as BookTypesService;


class BookTypes extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'typepage,addtype,updatepage,update'],
        'operationSelect'=>['only'=>'index,typelist,typepage,type,uppage,updatepage'],
        'operationUpdate'=>['only'=>'update']
    ];
    //图书类型的主页
    public function index()
    {
        return view('index');
    }
    //图书类型的主页 信息列表展示
    public function typeList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = DataCategory::getTypes($page,$rows,$sort,$sortOrder,$searchName);
        return $result;
    }
    //添加 图书类型 页面
    public function typePage()
    {
        $type = DataCategory::field("id,level,pid,catname")->select();
        return view("add",compact("type"));
    }
    //添加 图书类型
    public function addType(Request $request)
    {
        if($request->isAjax()){
            $data = BookTypesService::addType();
            if(!$data){
                return json(['msg'=>'添加失败', 'errorCode'=>203]);
            }
            return json(['code'=>200,'msg'=>'添加成功']);
        }
    }
    //修改 图书类型 信息
    public function updatePage()
    {
        $id   = Request::instance()->only('id');
        //分类信息
        $data = DataCategory::where('id',$id['id'])->find();
        //分类列表
        $type = DataCategory::field("id,level,pid,catname")->select();
        //父级类型的信息
        $parent = DataCategory::where("id",$data['pid'])->find();
        return view('update',compact("data","type","parent"));
    }
    //修改 图书类型
    public function update(Request $request)
    {
        if($request->isAjax()){
            $data = BookTypesService::update();
            if(!$data){
                return json(['msg'=>'修改失败', 'errorCode'=>203]);
            }
            return json(['code'=>200,'msg'=>'修改成功']
            );
        }
    }

}